package com.example.treehole.ui.page.createPost

import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.imePadding
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.text.BasicTextField
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.ArrowDropDown
import androidx.compose.material3.Button
import androidx.compose.material3.ButtonDefaults
import androidx.compose.material3.DropdownMenu
import androidx.compose.material3.DropdownMenuItem
import androidx.compose.material3.HorizontalDivider
import androidx.compose.material3.Icon
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.lifecycle.viewmodel.compose.viewModel
import com.example.treehole.LocalNavController
import com.example.treehole.LocalPostListViewModel
import com.example.treehole.LocalUserViewModel
import com.example.treehole.ui.model.PostType
import com.example.treehole.ui.model.SquareViewModel
import com.example.treehole.ui.theme.backCyan
import com.example.treehole.ui.theme.darkCyan
import com.example.treehole.ui.theme.postCyan
import kotlinx.coroutines.launch

@Composable
fun CreatePostScreen() {
    val navController = LocalNavController.current
    val userViewModel = LocalUserViewModel.current
    val userId by userViewModel.userId.collectAsState()
    val jwlToken by userViewModel.jwlToken.collectAsState()
    val scope = rememberCoroutineScope()

    val postListViewModel = LocalPostListViewModel.current

    val createPostViewModel: CreatePostViewModel = viewModel()
    val squareViewModel: SquareViewModel = viewModel()
    val title = createPostViewModel.title
    val selectedTag = createPostViewModel.selectedTag
    val content = createPostViewModel.content

    var expanded by remember { mutableStateOf(false) }

    Box(modifier = Modifier
        .fillMaxSize()
        .background(postCyan)
        .imePadding()) {
        Column(modifier = Modifier.fillMaxSize()) {
            Row(
                modifier = Modifier
                    .fillMaxWidth()
                    .background(backCyan),
                horizontalArrangement = Arrangement.SpaceBetween
            ) {
                Button(
                    onClick = { navController.popBackStack() },
                    colors =
                    ButtonDefaults.buttonColors(
                        containerColor = Color.Transparent,
                        contentColor = darkCyan
                    )
                ) { Text("取消", fontSize = 18.sp) }
                Button(
                    onClick = {
                        scope.launch {
                            userId?.let {
                                if (createPostViewModel.uploadPost(jwlToken, it)) {
                                    createPostViewModel.clearData()
                                    navController.popBackStack()
                                    postListViewModel.refreshLatestPostList(jwlToken, it)
                                }
                            }
                        }
                    },
                    colors =
                    ButtonDefaults.buttonColors(
                        containerColor = Color.Transparent,
                        contentColor = darkCyan
                    )
                ) { Text("发布", fontSize = 18.sp) }
            }
            BasicTextField(
                value = title.value,
                onValueChange = { createPostViewModel.updateTitle(it) },
                textStyle = TextStyle(fontSize = 18.sp, color = Color.Black),
                modifier =
                Modifier
                    .fillMaxWidth()
                    .padding(horizontal = 16.dp)
                    .padding(top = 8.dp),
                decorationBox = { innerTextField ->
                    Row(modifier = Modifier.fillMaxWidth()) {
                        Text(
                            text = "标题：",
                            style = TextStyle(fontSize = 18.sp, color = Color.Black)
                        )
                        innerTextField()
                    }
                }
            )
            HorizontalDivider(
                modifier = Modifier.padding(vertical = 8.dp),
                color = Color.Gray,
                thickness = 1.dp
            )
            Column(modifier = Modifier
                .fillMaxWidth()
                .padding(horizontal = 16.dp)) {
                Row(
                    modifier = Modifier
                        .fillMaxWidth()
                        .clickable { expanded = !expanded },
                    horizontalArrangement = Arrangement.SpaceBetween
                ) {
                    Text(
                        text =
                        "标签：" +
                                (if (selectedTag.value == PostType.NO_TYPE) "无"
                                else
                                    squareViewModel.getTagNameByPostType(
                                        selectedTag.value
                                    )),
                        fontSize = 18.sp
                    )
                    Icon(
                        imageVector = Icons.Default.ArrowDropDown,
                        contentDescription = "select tag"
                    )
                }

                Box(modifier = Modifier
                    .fillMaxWidth()
                    .padding(start = 16.dp)) {
                    DropdownMenu(
                        expanded = expanded,
                        onDismissRequest = { expanded = false },
                        modifier = Modifier.background(postCyan)
                    ) {
                        squareViewModel.tags.forEach { tagSection ->
                            DropdownMenuItem(
                                text = {
                                    Box(
                                        modifier = Modifier.fillMaxWidth() // 让 Box 填满整个可用宽度
                                    ) {
                                        Text(
                                            text =
                                            if (tagSection.title == "") "无"
                                            else tagSection.title,
                                            fontSize = 16.sp,
                                            modifier = Modifier.align(Alignment.Center)
                                        )
                                    }
                                },
                                onClick = {
                                    createPostViewModel.updateSelectedTag(tagSection.type)
                                    expanded = false
                                }
                            )
                        }
                    }
                }
            }
            HorizontalDivider(
                modifier = Modifier.padding(vertical = 8.dp),
                color = Color.Gray,
                thickness = 1.dp
            )
            BasicTextField(
                value = content.value,
                onValueChange = { createPostViewModel.updateContent(it) },
                textStyle = TextStyle(fontSize = 16.sp, color = Color.Black),
                modifier = Modifier
                    .fillMaxWidth()
                    .padding(16.dp),
                decorationBox = { innerTextField ->
                    Box(modifier = Modifier.fillMaxWidth()) {
                        if (content.value.isEmpty()) {
                            Text(text = "输入帖子内容吧~", color = Color.Gray)
                        }
                        innerTextField()
                    }
                }
            )
        }
    }
}
